﻿<navCtl:Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:navCtl="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
  xmlns:fx="clr-namespace:BookClub.Core"
  xmlns:fxCtl="clr-namespace:BookClub.Controls"
  xmlns:local="clr-namespace:BookClub.Pages"
  x:Class="BookClub.Pages.BookShelf" 
  mc:Ignorable="d"
  d:DataContext="{d:DesignInstance Type=local:BookShelfModel, IsDesignTimeCreatable=False}"
  d:DesignWidth="800" d:DesignHeight="600"
  Title="BookShelf">
  <navCtl:Page.Resources>
    <fx:Command x:Key="loadCommand" Method="LoadBooks" />
    <fx:Command x:Key="moreCommand" Method="LoadMoreBooks" />
    <fx:Command x:Key="saveCommand" Method="SaveBooks" />
    <fx:Command x:Key="shareCommand" Method="ShareBook" />
    <fx:Command x:Key="unshareCommand" Method="UnshareBook" />
    <fx:Command x:Key="editCommand" Method="EditBook" />
    <fx:Command x:Key="commitEditCommand" Method="CommitEditing" />
    <fx:Command x:Key="cancelEditCommand" Method="CancelEditing" />
  </navCtl:Page.Resources>

  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="325"/>
      <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <StackPanel Orientation="Horizontal" Grid.ColumnSpan="2">
      <TextBox x:Name="searchText" Width="200" Margin="0,0,4,0" />
      <Button Content="Load" Margin="0,0,4,0"
        Command="{StaticResource loadCommand}"
        CommandParameter="{Binding ElementName=searchText, Path=Text}"/>
      <Button Content="Save"
        IsEnabled="{Binding HasChanges}"
        Command="{StaticResource saveCommand}" />
    </StackPanel>
    <StackPanel Orientation="Horizontal" Grid.ColumnSpan="2" HorizontalAlignment="Right">
      <Button Content="Share a Book" Width="100"
        Visibility="{Binding IsEditing, Converter={StaticResource invisibilityConverter}}"
        Command="{StaticResource shareCommand}" />
    </StackPanel>


    <ListBox Grid.Row="1"
      ItemsSource="{Binding Books}"
      SelectedItem="{Binding SelectedBook, Mode=TwoWay}"
      Background="{x:Null}" Margin="0,5,0,32"
      ScrollViewer.HorizontalScrollBarVisibility="Disabled"
      ScrollViewer.VerticalScrollBarVisibility="Auto">
      <ListBox.ItemTemplate>
        <DataTemplate>
          <Grid>
            <TextBlock Style="{StaticResource listItemText}" Margin="20,6,5,5"
              Text="{Binding Title}" />
            <Rectangle Style="{StaticResource newMarker}"
              Visibility="{Binding IsNew, Converter={StaticResource visibilityConverter}}" />
            <Rectangle Style="{StaticResource modifiedMarker}"
              Visibility="{Binding IsModified, Converter={StaticResource visibilityConverter}}" />
            <Rectangle Style="{StaticResource errorMarker}"
              Visibility="{Binding HasValidationErrors, Converter={StaticResource visibilityConverter}}" />
          </Grid>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
    <fxCtl:ActivityControl Grid.Row="1" Height="28" VerticalAlignment="Bottom" Margin="0,0,80,0"
      Operation="{Binding CurrentOperation}" />
    <Button Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="0,0,0,3"
      Content="More"
      Command="{StaticResource moreCommand}"/>

    <Grid Grid.Row="1" Grid.Column="1" Margin="20,5,0,5"
      Visibility="{Binding SelectedBook, Converter={StaticResource visibilityConverter}}">
      <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
      </Grid.RowDefinitions>
      <fxCtl:BookDetails Book="{Binding SelectedBook}"
        Visibility="{Binding IsEditing, Converter={StaticResource invisibilityConverter}}"/>
      <fxCtl:BookEditor Book="{Binding SelectedBook}" Categories="{Binding Categories}"
        Visibility="{Binding IsEditing, Converter={StaticResource visibilityConverter}}"/>

      <StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Right">
        <Button Content="Stop Sharing" Margin="0,0,4,0" Width="100"
          Command="{StaticResource unshareCommand}" />
        <Button Content="Edit" Margin="0,0,4,0"
          Visibility="{Binding IsEditing, Converter={StaticResource invisibilityConverter}}"
          Command="{StaticResource editCommand}" />
        <Button Content="OK" Margin="0,0,4,0"
          Visibility="{Binding IsEditing, Converter={StaticResource visibilityConverter}}"
          Command="{StaticResource commitEditCommand}" />
        <Button Content="Cancel"
          Visibility="{Binding IsEditing, Converter={StaticResource visibilityConverter}}"
          Command="{StaticResource cancelEditCommand}"/>
      </StackPanel>
    </Grid>
  </Grid>
</navCtl:Page>
